home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
util
/
merge.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
3KB
|
93 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from operator import isMappingType, isSequenceType
from primitives import Storage as S
if 'sentinel' not in globals():
sentinel = object()
def merge(*mappings, **opts):
d = { }
keytransform = opts.get('keytransform', (lambda k: k))
for elem in mappings:
if isMappingType(elem):
items = elem.iteritems()
elif isSequenceType(elem):
if not all((lambda .0: for s in .0:
len(s) == 2)(elem)):
raise TypeError('mapping sequences must be sequences of (key,value) pairs: %s %s' % (type(elem), repr(elem)))
items = elem
else:
raise TypeError('all arguments to merge must be mappings: %r' % elem)
for key, value in items:
merge_values(d, keytransform(key), value, keytransform)
return d
def merge_values(mapping, key, value, keytransform = None):
if keytransform is None:
keytransform = lambda k: k
curval = mapping.get(key, sentinel)
if curval is not sentinel and isMappingType(curval) and isMappingType(value):
for subkey, subvalue in value.iteritems():
merge_values(curval, keytransform(subkey), subvalue)
else:
mapping[keytransform(key)] = value
def lower_if_string(s):
try:
return s.lower()
except AttributeError:
return s
def merge_keys(d):
if not d:
return d
return tolower(merge(d, keytransform = lower_if_string))
def tolower(mapping):
return S((lambda .0: for k, v in .0:
(None, getattr(k, 'lower', (lambda k: k))() if isMappingType(v) else v)
)(mapping.iteritems()))
def main():
d = {
1: 2 }
merge_values(d, 3, 4)
d = {
1: 2 }
merge_values(d, 1, 3)
d = {
1: {
2: 3 } }
d2 = {
1: {
2: 4 } }
res = merge(d, d2)
d = {
1: {
2: 3 } }
d2 = [
(1, {
2: 4 })]
res = merge(d, d2)
if __name__ == '__main__':
main()